var common = require("../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        topImgWidth: 0,
        topImgHeight: 0,
        my_leaderID: "",
        productId: "",
        timeId: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var that = this;
        wx.showLoading({
            title: "二维码生成中"
        });
        let canvasImg = options.img || "/images/share.jpg";
        let canvasTitle = options.title || "更多好物等你来拿";
        let canvasPrice = options.price || "";
        let my_leaderID = options.my_leaderID || "";
        let timeId = options.timeId || "";
        let productId = options.productId;
        that.setData({
            canvasImg: canvasImg,
            canvasTitle: canvasTitle,
            canvasPrice: "￥" + canvasPrice,
            my_leaderID: my_leaderID,
            productId: productId,
            timeId: timeId
        }), that.canvas()
        /**
   * 生命周期函数--监听页面初次渲染完成
   */;
    },
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    canvas: function() {
        var rpx;
        var that = this;
        var timeId = that.data.timeId;
        wx.getSystemInfo({
            success: function(res) {
                rpx = res.windowWidth / 375;
            }
        });
        var my_leaderID = that.data.my_leaderID;
        var productId = that.data.productId;
        if (timeId == "") {
            var url = "pages/pro_detail/pro_detail";
        } else {
            var url = "pages/kill_detail/kill_detail";
        }
        var canvasImg = "";
        var proimg;
        var topImgWidth, topImgHeight;
        //不是https的
                if (that.data.canvasImg.indexOf("https") == -1) {
            proimg = "https://" + that.data.canvasImg.substring(7);
        } else {
            proimg = that.data.canvasImg;
        }
        wx.getImageInfo({
            src: proimg,
            success: res => {
                topImgWidth = res.width;
                topImgHeight = res.height;
                canvasImg = res.path;
                common.request({
                    endpoint: "My/qrcode/two_dimension_code",
                    method: "post",
                    data: {
                        page: url,
                        pro_id: productId,
                        leader_id: my_leaderID,
                        time_id: timeId
                    },
                    success: res => {
                        var ewmimg = res.extra;
                        wx.getImageInfo({
                            src: ewmimg,
                            success: res => {
                                wx.hideLoading();
                                var ph = 690 * topImgHeight / topImgWidth / 2;
                                const ctx = wx.createCanvasContext("myCanvas");
                                //图片
                                                                ctx.drawImage(canvasImg, 0, 0, 345 * rpx, ph * rpx);
                                //底部白色矩形
                                                                ctx.setFillStyle("#ffffff");
                                ctx.fillRect(0, ph * rpx, 345 * rpx, 145 * rpx);
                                //文字开始
                                                                ctx.setFillStyle("#000000");
                                ctx.font = "14px bold";
                                // ctx.fillText(that.data.canvasTitle, 20 * rpx, (ph + 35) * rpx);
                                                                that.textLine(ctx, that.data.canvasTitle, 20 * rpx, (ph + 35) * rpx, 150 * rpx);
                                //文字开始
                                                                ctx.setFillStyle("#ff7b19");
                                ctx.font = "18px bold";
                                ctx.fillText(that.data.canvasPrice, 20 * rpx, (ph + 68) * rpx);
                                //下方带边框矩形
                                                                ctx.setFillStyle("#ffffff");
                                ctx.setStrokeStyle("#47b34f");
                                ctx.strokeRect(20 * rpx, (ph + 86) * rpx, 124 * rpx, 34 * rpx);
                                // ctx.fillRect(29 * rpx, (ph + 57) * rpx, 127 * rpx, 30 * rpx)  //导入矩形框子
                                //矩形内文字
                                                                ctx.font = "14px bold";
                                ctx.setFillStyle("#47b34f");
                                ctx.fillText("长按二维码抢购", 31 * rpx, (ph + 108) * rpx);
                                //二维码
                                                                ctx.drawImage(res.path, 220 * rpx, (ph + 15) * rpx, 110 * rpx, 110 * rpx);
                                ctx.draw();
                                that.setData({
                                    canvaHeight: (ph + 165) * rpx * 2
                                });
                            }
                        });
                    }
                });
            }
        });
    },
    textLine: function(ctx, text, x, y, maxWidth) {
        var chr = text.split("");
        //这个方法是将一个字符串分割成字符串数组
                var temp = "";
        var row = [];
        for (var c = 0; c < chr.length; c++) {
            if (ctx.measureText(temp).width < maxWidth) {
                temp += chr[c];
            } else {
                c--;
                //这里添加了a-- 是为了防止字符丢失，效果图中有对比
                                row.push(temp);
                temp = "";
            }
        }
        row.push(temp);
        //如果数组长度大于2 则截取前两个
                if (row.length > 1) {
            var rowCut = row.slice(0, 1);
            var rowPart = rowCut[0];
            var test = "";
            var empty = [];
            for (var d = 0; d < rowPart.length; d++) {
                if (ctx.measureText(test).width < maxWidth) {
                    test += rowPart[d];
                } else {
                    break;
                }
            }
            empty.push(test);
            var group = empty[0] + "...";
            //这里只显示两行，超出的用...表示
                        rowCut.splice(0, 1, group);
            row = rowCut;
        }
        for (var f = 0; f < row.length; f++) {
            if (f == 0) {
                ctx.fillText(row[f], x, y);
                // } else {
                //   ctx.fillText(row[f], x, y + 20);
                        }
        }
    },
    saveimgdo: function() {
        var that = this;
        wx.getSetting({
            success(res) {
                if (!res.authSetting["scope.writePhotosAlbum"]) {
                    wx.authorize({
                        scope: "scope.writePhotosAlbum",
                        success() {
                            that.dosave();
                        },
                        fail() {
                            wx.openSetting();
                            wx.showModal({
                                title: "提示",
                                content: "未授权相册，请删小程序后再试！"
                            });
                        }
                    });
                } else {
                    that.dosave();
                }
            }
        });
    },
    dosave: function() {
        var that = this;
        var pwidth = that.data.pwidth;
        var pheight = that.data.heigts;
        var pixelRatio = that.data.pixelRatio;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: pwidth,
            height: pheight,
            destWidth: pwidth * pixelRatio,
            destHeight: pheight * pixelRatio,
            canvasId: "myCanvas",
            success: function(res) {
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function(res) {
                        wx.showModal({
                            title: "提醒",
                            content: "请转发您分享图给好友吧！",
                            showCancel: false,
                            success: function(res) {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }
                        });
                    },
                    fail: function(res) {}
                });
            }
        });
    }
});